package org.wuyichun.eduserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wuyichun.base.util.AjaxResult;
import org.wuyichun.base.util.PageInfo;
import org.wuyichun.eduserver.domain.Employee;
import org.wuyichun.eduserver.query.EmployeeQuery;
import org.wuyichun.eduserver.service.IEmployeeService;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @PutMapping
    private AjaxResult save(@RequestBody Employee employee){
        try {
            if (Objects.isNull(employee.getId()))employeeService.insert(employee);
            else employeeService.update(employee);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    @GetMapping("/{id}")
    private AjaxResult selectById(@PathVariable Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return  AjaxResult.me().setResultObj(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    @GetMapping
    private AjaxResult selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return  AjaxResult.me().setResultObj(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除")
    private  AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            employeeService.batchDelete(ids);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "高级查询+分页")
    private AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery) {
        try {
            PageInfo<Employee> pageInfo = employeeService.queryPage(employeeQuery);
            return AjaxResult.me().setResultObj(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
}
